<?php

/**
 * This is the model class for table "{{category}}".
 *
 * The followings are the available columns in table '{{category}}':
 * @property string $id
 * @property string $title
 * @property string $name
 * @property string $desc
 * @property string $parentid
 * @property integer $display
 * @property integer $hot
 * @property integer $localeid
 * @property string $updatetime
 * @property string $updateuserid
 * @property string $createtime
 * @property string $createuserid
 *
 * The followings are the available model relations:
 * @property Locale $locale
 * @property CategoryArticle[] $categoryArticles
 */
class Category extends ActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Category the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{category}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title, name, localeid', 'required'),
			array('display, hot, localeid', 'numerical', 'integerOnly'=>true),
			array('title, name', 'length', 'max'=>255),
			array('desc', 'length', 'max'=>1024),
			array('parentid, updateuserid, createuserid', 'length', 'max'=>11),
			array('updatetime, updateuserid, createuserid, createtime', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, name, desc, parentid, display, hot, localeid, updatetime, updateuserid, createtime, createuserid', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'locale' => array(self::BELONGS_TO, 'Locale', 'localeid'),
			'articleid' => array(self::MANY_MANY, 'Article', '{{category_article}}(categoryid,articleid)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Title',
			'name' => 'Name',
			'desc' => 'Desc',
			'parentid' => 'Parentid',
			'display' => 'Display',
			'hot' => 'Hot',
			'localeid' => 'Localeid',
			'updatetime' => 'Updatetime',
			'updateuserid' => 'Updateuserid',
			'createtime' => 'Createtime',
			'createuserid' => 'Createuserid',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('desc',$this->desc,true);
		$criteria->compare('parentid',$this->parentid,true);
		$criteria->compare('display',$this->display);
		$criteria->compare('hot',$this->hot);
		$criteria->compare('localeid',$this->localeid);
		$criteria->compare('updatetime',$this->updatetime,true);
		$criteria->compare('updateuserid',$this->updateuserid,true);
		$criteria->compare('createtime',$this->createtime,true);
		$criteria->compare('createuserid',$this->createuserid,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
        public function getSeriesOptions(){
            $seriesArray = CHtml::listData(Locale::model()->findAll(), 'id', 'desc');
            return $seriesArray;
        }
        
        public function getParentCategoryOptions(){
            if($this->id !== NULL){
                $categoryArray = CHtml::listData(Category::model()->findAll('id != '.$this->id), 'id', 'title');
            }
            else{
                $categoryArray = CHtml::listData(Category::model()->findAll(), 'id', 'title');
            }
            return $categoryArray;
        }
}